home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / ada / print.me < prev    next >
Text File  |  1988-03-25  |  72KB  |  1,703 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.                                      Welcome  to
  22.                                    A D A - T U T R
  23.                               The Interactive Ada Tutor
  24.                                by John J. Herro, Ph.D.
  25.                            Software Innovations Technology
  26.  
  27.                          These are the printed course notes.
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.                                     25 March 1988
  44.  
  45.                             Copyright 1988  John J. Herro
  46.  
  47.                            Software Innovations Technology
  48.                     1083 Mandarin Dr. NE, Palm Bay, FL 32905-4706
  49.                                     (407)951-0233
  50.  
  51.  
  52.                          You may make copies of these notes,
  53.                          in printed or machine-readable form.
  54.  
  55.                        You may also copy the computer program.
  56.                            Please see page ii for details.
  57.  
  58.  
  59.                                                                       Page  i
  60.  
  61.                                  TABLE OF CONTENTS
  62.  
  63.  
  64.        Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   i
  65.  
  66.        Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . .  ii
  67.  
  68.        Registration and Licenses - What is Shareware? . . . . . . . . . .   1
  69.  
  70.        Some Ada Compilers Available for the PC  . . . . . . . . . . . . .   2
  71.  
  72.        Before You Run ADA-TUTR on Your PC . . . . . . . . . . . . . . . .   4
  73.  
  74.        Installing ADA-TUTR on Other Computers . . . . . . . . . . . . . .   5
  75.  
  76.        The Ada Reserved Words . . . . . . . . . . . . . . . . . . . . . .   6
  77.  
  78.        Steps for Outside Assignment 1, Preparing to Run Ada . . . . . . .   7
  79.  
  80.        Listings of HELLO.ADA and ADD.ADA  . . . . . . . . . . . . . . . .   8
  81.  
  82.        Listing of TRITEST.ADA . . . . . . . . . . . . . . . . . . . . . .   9
  83.  
  84.        Steps for Outside Assignment 2, Exercise in Enumeration Types  . .  11
  85.  
  86.        Listing of NEXTDATE.ADA  . . . . . . . . . . . . . . . . . . . . .  12
  87.  
  88.        Steps for Outside Assignment 3, Exercise in Records  . . . . . . .  13
  89.  
  90.        Listing of FIBTEST.ADA . . . . . . . . . . . . . . . . . . . . . .  14
  91.  
  92.        Steps for Outside Assignment 4, Exercise in Recursion  . . . . . .  15
  93.  
  94.        Simplified Specification for TEXT_IO . . . . . . . . . . . . . . .  16
  95.  
  96.        Listings of Procedure COPY and Function EXISTS . . . . . . . . . .  18
  97.  
  98.        Requirements for the Program LEDIT . . . . . . . . . . . . . . . .  19
  99.  
  100.        Steps for Outside Assignment 5, Writing a Simple Line Editor . . .  24
  101.  
  102.        How to Test LEDIT  . . . . . . . . . . . . . . . . . . . . . . . .  25
  103.  
  104.        Listing of LEDIT.ANS . . . . . . . . . . . . . . . . . . . . . . .  28
  105.  
  106.        Listing of TASKING.DUM . . . . . . . . . . . . . . . . . . . . . .  32
  107.  
  108.        Steps for Outside Assignment 6, Exercise in Tasking  . . . . . . .  33
  109.  
  110.        Output from TASKING.EXE After Modification of TASKING.ADA  . . . .  34
  111.  
  112.        Listing of TASKING.ANS . . . . . . . . . . . . . . . . . . . . . .  35
  113.  
  114.        Disclaimer of Warranty . . . . . . . . . . . . . . . . . . . . . .  36
  115.  
  116.                                                                       Page ii
  117.  
  118.                                     INTRODUCTION
  119.  
  120.        ADA-TUTR, the Interactive Ada Tutor, will make you an excellent Ada
  121.        programmer in minimum time.  You'll learn good Ada program design
  122.        techniques, not just Ada syntax.  ADA-TUTR runs on PCs as well as
  123.        large mainframes.  On PCs an Ada compiler is helpful, but not
  124.        required.  The PC needs a hard disk or a 3 1/2" disk, and can have a
  125.        monochrome or a color monitor.
  126.  
  127.        These printed notes aren't meant to be a complete course, or even a
  128.        summary, of Ada.  They merely accompany the program ADA-TUTR, which is
  129.        a complete course.  You can't "study" these printed notes alone.  For
  130.        now, just read through page 3, and read pages 4 and 5 if they apply.
  131.  
  132.        ADA-TUTR lets you learn at your own pace.  So that you don't feel
  133.        pressured, ADA-TUTR doesn't keep track of the number of right and
  134.        wrong answers.  It simply tells you whether your answers are correct,
  135.        and why.  Also, there's no time limit for answering the questions or
  136.        completing the Outside Assignments.
  137.  
  138.        Because I want every programmer to have a chance to learn the best
  139.        programming language, I made ADA-TUTR available very inexpensively as
  140.        Shareware.  Shareware isn't the same as public domain, and ADA-TUTR
  141.        isn't free.  But you may TRY it for free, and register or buy a
  142.        license only if you use it.  Please see page 1 for details.
  143.  
  144.        Whether or not you use ADA-TUTR and register (or buy a license),
  145.        you're encouraged to make unmodified copies and give them away.
  146.        Please put ADA-TUTR on computer bulletin boards, distribute copies at
  147.        club meetings, give them to companies, etc.!  You may charge a small
  148.        fee to copy the program, provided you make it clear that the fee is
  149.        only for the copy, and doesn't pay for the Shareware.  (Companies that
  150.        copy Shareware usually charge about $3 to $10 per diskette.)  Bulletin
  151.        boards may carry ADA-TUTR even if they charge for access.  Please
  152.        contact us for permission to include ADA-TUTR with commercial
  153.        products, such as Ada compilers (see page 3).  You may use file
  154.        compression, library, and archive programs on ADA-TUTR; we won't
  155.        interpret that as "modifying" the program.  You may also add your own
  156.        files.  (The files included with ADA-TUTR are briefly described in
  157.        ED.DIR.)  If you register, you can earn substantial money distributing
  158.        ADA-TUTR; please see page 1 for details.
  159.  
  160.        Ada will become more and more important, since the Department of
  161.        Defense mandated its use in mission-critical systems, and is now
  162.        making it very hard to obtain waivers.  Although learning Ada takes
  163.        real effort, you'll be able to write software that's more reliable
  164.        and easier to modify a month or a year later.
  165.  
  166.        Please send me your comments and suggestions.  I wish you success
  167.        using ADA-TUTR!
  168.  
  169.  
  170.        John J. Herro, Ph.D., Software Innovations Technology
  171.        1083 Mandarin Drive NE, Palm Bay, FL 32905-4706   (407)951-0233
  172.  
  173.                                                                       Page  1
  174.  
  175.                    REGISTRATION AND LICENSES - WHAT IS SHAREWARE?
  176.  
  177.        Shareware is a way of marketing a program.  It lets you try the
  178.        program before spending any money.  If you decide to use it, you send
  179.        a small payment to register your copy or buy a license.  We give you
  180.        several incentives to do so, explained below.  You're on your honor;
  181.        you know whether you're "using" ADA-TUTR or only "trying" it.
  182.  
  183.        When registering or buying a license, please give us the serial number
  184.        from the screen that comes up when you start ADA-TUTR.  We'll assign
  185.        you a new serial number.  You'll have the right to use ADA-TUTR as
  186.        long as you like, and you'll get technical support for one year (it
  187.        can be renewed very inexpensively).  During that time, we'll inform
  188.        you of any important updates to ADA-TUTR, and send you updated copies
  189.        for only $5 each (or FREE if you send a blank, formatted, 5 1/4" DSDD
  190.        diskette and a stamped, addressed diskette mailer).  We're planning a
  191.        major update of ADA-TUTR with the next revision of the Ada language.
  192.        If you wish, you may order the current version of ADA-TUTR (with your
  193.        serial number) when you register or buy a license.  If you need or
  194.        want an invoice (or an order form), please print the file INVOICE.TXT.
  195.        We sell anywhere in the Free World; outside the U.S., please remit in
  196.        U.S. funds and contact us about extra postal charges on diskettes.
  197.  
  198.        INDIVIDUALS:  Register your copy for only $25.  When you get your new
  199.        serial number, run CHANGESN to put it into the program.  Then give
  200.        copies to individuals, companies, schools, clubs, computer bulletin
  201.        boards, etc.  We'll send you a commission of $5 for each individual
  202.        who registers from one of your copies, and 10 PERCENT for each license
  203.        sold from one of your copies!  As you can see from the next paragraph,
  204.        you can earn up to $240 per license sold!  Commissions are paid
  205.        quarterly.  If you like, print the file AD.TXT and use copies as an
  206.        advertisement.  You can renew technical support for only $10 a year.
  207.        Even if you don't renew, you'll continue to receive commissions.
  208.  
  209.        COMPANIES, SCHOOLS, OTHER ORGANIZATIONS:  Register each individual
  210.        user, as described above, or buy a Multi-User License as follows:
  211.  
  212.                    Number of Users:             Price of License:
  213.                       Up to  100                   only  $450
  214.                       Up to  500                   only  $725
  215.                       Up to 2000                   only  $950
  216.                       Unlimited                    only $2400
  217.  
  218.        This gives the specified number of people the right to use ADA-TUTR as
  219.        long as you like, on all your computers, from PCs to large mainframes.
  220.        Page 5 shows how to install ADA-TUTR on non-PC computers.  Commissions
  221.        are NOT paid to Multi-User Licensees.  Renewal of technical support is
  222.        only 10% of the price of the license per year.  With Shareware, you
  223.        never have to worry about software piracy, because your people are
  224.        ENCOURAGED to copy and distribute the program!
  225.  
  226.                           Software Innovations Technology
  227.                    1083 Mandarin Dr. NE, Palm Bay, FL 32905-4706
  228.                                    (407)951-0233
  229.  
  230.                                                                       Page  2
  231.  
  232.                       SOME ADA COMPILERS AVAILABLE FOR THE PC
  233.  
  234.        Here's a brief list of Ada compilers available for the PC and
  235.        compatibles.  For this course, an Ada compiler is helpful, but not
  236.        required.  There are six Outside Assignments; most of them ask you to
  237.        write and compile a short Ada program.  If you don't have access to an
  238.        Ada compiler, just skip the Outside Assignments.
  239.  
  240.        All of the compilers in this list run on a PC/XT or larger machine,
  241.        except as noted.
  242.  
  243.        We have no financial interest in any company mentioned here.  If you
  244.        know of an Ada compiler for the PC that you think should be on this
  245.        list, please contact us.
  246.  
  247.        All of the company names and all of the compiler names except Augusta
  248.        are trademarks of the respective companies.
  249.  
  250.        1.  At the low end of the scale is Augusta.  This compiler is in the
  251.            public domain, and can be found on many computer bulletin boards.
  252.            It's a very small subset of Ada with no packages.  It translates
  253.            Ada to a P-code, and then interprets the P-code.  The spelling of
  254.            the Ada reserved word "elsif" is non-standard; Augusta uses
  255.            "elseif."  The compiler requires Turbo Pascal (tm Borland
  256.            International).
  257.  
  258.        2.  ADA/Ed-C is a full Ada "translator" available from NYUADA Project,
  259.            New York University, 251 Mercer St., New York, NY  10012, for $95.
  260.            It translates Ada to an intermediate code and then interprets.  On
  261.            a PC/XT, programs larger than a few hundred lines run out of
  262.            memory, but on a PC/AT, the translator passes all the tests of the
  263.            validation suite.
  264.  
  265.        3.  Janus/Ada is now validated, and is sold by R & R Software, Box
  266.            1512, Madison, WI  53701, starting at $99.95.  It compiles to
  267.            native code, and doesn't need an interpreter.
  268.  
  269.        4.  Artek Ada is sold for $495 by Artek Corp., 835 E. 25th Ave.,
  270.            Eugene, OR  97405.  It's nearly a full Ada, and can produce native
  271.            code.  It includes a source code debugger, the textbook by S. J.
  272.            Young mentioned in ADA-TUTR, and a copy of the LRM.  As of this
  273.            writing (March, 1988), they're working on a validated version,
  274.            which will be sold by a well-known company whose name they haven't
  275.            yet revealed.
  276.  
  277.        5.  AdaVantage is sold by Meridian Software, Inc., 23141 Verdugo Dr.,
  278.            Suite 105, Laguna Hills, CA  92653, starting at $795.  It's
  279.            validated and produces native code.  A "starter" version which
  280.            compiles small programs is available for $95.
  281.  
  282.  
  283.                                   -- continued --
  284.  
  285.                                                                       Page  3
  286.  
  287.        6.  At the high end of the scale is Alsys Ada, sold by Alsys, Inc.,
  288.            1432 Main St., Waltham, MA  02154, for $3300.  It's a validated
  289.            Ada that runs only on a PC/AT, but can produce native code for the
  290.            PC/XT.  The price includes an extra memory board for the AT.
  291.  
  292.  
  293.  
  294.                                 COMPILER COMPANIES:
  295.  
  296.        A commercial version of ADA-TUTR is available, without the Shareware
  297.        notices and without this list of Ada compilers.  You can greatly
  298.        increase the sales appeal and value of your Ada compiler by including
  299.        the commercial version of ADA-TUTR.  Contact us for terms.
  300.  
  301.  
  302.  
  303.                    DO YOU NEED A SCREEN EDITOR / WORD PROCESSOR?
  304.  
  305.        The Outside Assignments will be much easier if you have a good screen
  306.        editor.  We've looked at many, and found PC-Write (tm), a Shareware
  307.        program by Quicksoft (tm) of Seattle, WA to be the best screen editor
  308.        as well as word processor.  There's lots of on-line help, and the
  309.        50000-word spelling checker can be turned on for editing letters and
  310.        documents, and off for editing source code.  We liked the idea of
  311.        having to learn only ONE program for both kinds of editing.  Unlike
  312.        ADA-TUTR, PC-Write runs only on PCs and compatibles, not on
  313.        mainframes, etc.
  314.  
  315.        We have no connection with Quicksoft, except for being a registered
  316.        user of PC-Write.  However, we'll be happy to send you a copy of the
  317.        latest version of that excellent program, if you send us two blank,
  318.        formatted, 5 1/4" DSDD diskettes and a stamped, addressed diskette
  319.        mailer, OR $10.  The $10 fee only covers our expenses; you must
  320.        register your copy of PC-Write with Quicksoft if you use it after a
  321.        free trial.  We're sorry, but we're not allowed to send copies of
  322.        PC-Write outside North America.
  323.  
  324.        If you do use PC-Write to edit Ada programs, you may find the file
  325.        ED.ADA, included with ADA-TUTR, to be helpful.  It allows typing many
  326.        Ada reserved words with a single keystroke.  For example, you can type
  327.        "procedure" by striking Alt-P.  See the file ED.ADA for more details.
  328.        PC-Write will automatically invoke ED.ADA whenever you edit a file
  329.        with the extension .ADA.
  330.  
  331.  
  332.                           Software Innovations Technology
  333.                    1083 Mandarin Dr. NE, Palm Bay, FL 32905-4706
  334.                                    (407)951-0233
  335.  
  336.                                                                       Page  4
  337.  
  338.                          BEFORE YOU RUN ADA-TUTR ON YOUR PC
  339.  
  340.        Give the command TYPE READ.ME on your PC.  If one word appears
  341.        brighter than the rest, you can skip this page.  However, if your
  342.        screen shows strange characters like "[1m", you need to read this.
  343.  
  344.        ADA-TUTR uses ANSI (American National Standards Institute) escape
  345.        sequences for highlighting, reverse video, and cursor positioning.
  346.        Therefore, the device driver ANSI.SYS must be installed before
  347.        ADA-TUTR will work correctly.  To install ANSI.SYS, do the following:
  348.  
  349.        1.  If there's a file CONFIG.SYS in the root directory of the disk
  350.            from which you boot, type it and look for a line saying
  351.            "DEVICE=ANSI.SYS" (without the quotes), in either upper or lower
  352.            case.  If that line is not present, add it to CONFIG.SYS anywhere
  353.            in the file, using an ordinary text editor or word processor in
  354.            the non-document mode.  If there's no CONFIG.SYS file, create one
  355.            containing the single line "DEVICE=ANSI.SYS" (without the quotes).
  356.  
  357.        2.  If there's no file ANSI.SYS in your root directory, copy ANSI.SYS
  358.            from from your system distribution diskette to the root directory
  359.            of the disk from which you boot.
  360.  
  361.        3.  Reboot the computer.  ADA-TUTR should then work correctly.
  362.  
  363.                                                                       Page  5
  364.  
  365.                        INSTALLING ADA-TUTR ON OTHER COMPUTERS
  366.  
  367.        Because source code is included with ADA-TUTR, you can install
  368.        ADA-TUTR on almost any computer that has an Ada compiler - from
  369.        portable units to large mainframes.  ADA-TUTR is written in Ada (of
  370.        course!), and Ada programs tend to be very portable.
  371.  
  372.        The screen should have at least 24 rows of 80 characters each, and
  373.        should support the ANSI escape sequences for highlighting, reverse
  374.        video, and cursor positioning.  Almost all computers qualify.
  375.  
  376.        You'll need a way to send files from your PC to the other computer.
  377.        Files are usually sent to large mainframes with the program KERMIT,
  378.        which is in the public domain.  One version of KERMIT runs on the PC,
  379.        another on the mainframe.  The two communicate with each other.
  380.  
  381.        Send all the files that come with ADA-TUTR, except ADA-TUTR.DAT and
  382.        the files ending in .EXE.  Don't try to send those files, because
  383.        they're not text files.  Then, create ADA-TUTR.DAT on the other
  384.        computer as follows:  Run DAT2TXT on the PC to translate the file
  385.        ADA-TUTR.DAT to a text file TUTOR.TXT.  Send TUTOR.TXT to the other
  386.        computer.  Then compile and run TXT2DAT.ADA on the other computer to
  387.        translate TUTOR.TXT to ADA-TUTR.DAT on that machine.  On a few
  388.        systems, our file names are illegal and you'll have to change the file
  389.        names following "NAME =>" inside TXT2DAT.ADA and ADA-TUTR.ADA.
  390.  
  391.        The next step is to compile, on the other computer, NON-PC.ADA and
  392.        then ADA-TUTR.ADA.  Then link, giving the name of the main program,
  393.        which is ADA_TUTR with an underline.  Some compilers call the linking
  394.        step "binding."  This will produce an executable file, which will be
  395.        called ADA_TUTR.EXE (with an underline) on most systems.  You should
  396.        rename ADA_TUTR.EXE as ADA-TUTR.EXE.  Finally, run ADA-TUTR.
  397.  
  398.        On some systems, you may have to strike ENTER (or RETURN or NEW-LINE)
  399.        after each response, even though the program says "You need not hit
  400.        ENTER."  Also note that some of the text refers to commands that are
  401.        meant for a PC, such as "COPY TRITYPE.DUM TRITYPE.ADA".  These
  402.        commands may have to be varied slightly to work on your system.
  403.  
  404.        So far as we know, the file NON-PC.ADA will work with any standard Ada
  405.        compiler.  However, if you're using VAX Ada (tm, Digital Equipment
  406.        Corp.), the file VAX.ADA will work work even better.  Compile it
  407.        instead of NON-PC.ADA.  If you compile NON-PC.ADA with VAX Ada, you'll
  408.        have to hit RETURN after each response.  If you compile VAX.ADA
  409.        instead, you won't have to hit RETURN, because VAX.ADA makes use of
  410.        the System Service routines.
  411.  
  412.        When you compile ADA-TUTR.ADA, if you get error messages saying that
  413.        QGET and CON_IO aren't found in the library, you probably forgot to
  414.        compile NON-PC.ADA or VAX.ADA first.
  415.  
  416.        We'll be happy to help you install ADA-TUTR and get it running, even
  417.        if you haven't registered or bought a license.  We want you to try
  418.        ADA-TUTR before paying for it.
  419.  
  420.                                                                       Page  6
  421.  
  422.                               THE ADA RESERVED WORDS
  423.  
  424.        The 63 reserved words are listed in the Language Reference Manual in
  425.        section 2.9, and are repeated here for convenience.
  426.  
  427.          abort         declare        generic      of             select
  428.          abs           delay          goto         or             separate
  429.          accept        delta                       others         subtype
  430.          access        digits         if           out
  431.          all           do             in                          task
  432.          and                          is           package        terminate
  433.          array                                     pragma         then
  434.          at            else                        private        type
  435.                        elsif          limited      procedure
  436.                        end            loop
  437.          begin         entry                       raise          use
  438.          body          exception                   range
  439.                        exit           mod          record         when
  440.                                                    rem            while
  441.                                       new          renames        with
  442.          case          for            not          return
  443.          constant      function       null         reverse        xor
  444.  
  445.                                                                       Page  7
  446.  
  447.                 STEPS FOR OUTSIDE ASSIGNMENT 1, PREPARING TO RUN ADA
  448.  
  449.        For the first Outside Assignment, learn enough about your Ada compiler
  450.        to compile and run the two simple programs HELLO.ADA and ADD.ADA.
  451.        Obviously, you have to do this before you can do the remaining Outside
  452.        Assignments.
  453.  
  454.        1.  Compile HELLO.ADA.  On most systems, after the Ada compiler is
  455.        installed and your library is created, this only involves typing
  456.        ADA HELLO or ADA HELLO.ADA.  On some systems, the compiler is invoked
  457.        from a menu or "APSE" (Ada Programming Support Environment), and you
  458.        must first specify HELLO as the name of the "working file."
  459.  
  460.        2.  Link, giving the name of the main program, HELLO.  This usually
  461.        involves typing LINK HELLO or BIND HELLO, perhaps with some options.
  462.        On one system, the command is BAMP HELLO, for Build Ada Main Program!
  463.  
  464.        3.  Run the program.  If your Ada compiler produces native code on a
  465.        PC, it created a file HELLO.EXE, and this step simply involves typing
  466.        HELLO.  On other systems, the command RUN HELLO is appropriate.  On
  467.        some systems, you have to invoke an interpreter to run the program.
  468.  
  469.        4.  Now compile, link, and run ADD.ADA.
  470.  
  471.        If your compiler implements a subset of Ada, you may have to modify
  472.        the programs in this and later Outside Assignments to get them to
  473.        compile.  Some compilers don't allow "separate" compilation (used in
  474.        later assignments), and you'll have to include the "separate"
  475.        subprograms inside the main program and compile them as one.  Some
  476.        compilers don't allow generic instantiation, and have another way of
  477.        displaying integers.  In that case, you'll have to modify ADD.ADA.  If
  478.        you're using a validated compiler, you shouldn't have to worry about
  479.        any of this, because all of the Outside Assignments are written in
  480.        standard Ada.
  481.  
  482.                                                                       Page  8
  483.  
  484.        HELLO.ADA
  485.        ---------
  486.  
  487.        with TEXT_IO; use TEXT_IO;
  488.        procedure HELLO is
  489.        begin
  490.           PUT_LINE("Hello!");
  491.        end HELLO;
  492.  
  493.  
  494.  
  495.        ADD.ADA
  496.        -------
  497.  
  498.        with TEXT_IO; use TEXT_IO;
  499.        procedure ADD is
  500.           package MY_INT_IO is new INTEGER_IO(INTEGER); use MY_INT_IO;
  501.        begin
  502.           PUT(2 + 2);
  503.           NEW_LINE;
  504.        end ADD;
  505.  
  506.                                                                       Page 9
  507.  
  508.        TRITEST.ADA
  509.        -----------
  510.  
  511.        with TEXT_IO; use TEXT_IO;
  512.        procedure TRITEST is
  513.           PASSED : BOOLEAN := TRUE;
  514.           type TRIANGLE is (EQUILATERAL, ISOSCELES, SCALENE, NOT_A_TRIANGLE);
  515.           function TRITYPE(LEN1, LEN2, LEN3 : in INTEGER) return TRIANGLE
  516.                                                                  is separate;
  517.           procedure COMPARE(A, B, C: in INTEGER; RIGHT_ANSWER : in TRIANGLE)
  518.                                                                  is separate;
  519.        begin
  520.           COMPARE( 3,  4,  5, SCALENE);
  521.           COMPARE( 6,  3,  4, SCALENE);
  522.           COMPARE( 4,  3,  6, SCALENE);
  523.           COMPARE( 3,  3,  3, EQUILATERAL);
  524.           COMPARE( 3,  3,  4, ISOSCELES);
  525.           COMPARE( 3,  4,  3, ISOSCELES);
  526.           COMPARE( 4,  3,  3, ISOSCELES);
  527.           COMPARE( 7,  7,  4, ISOSCELES);
  528.           COMPARE( 7,  4,  7, ISOSCELES);
  529.           COMPARE( 4,  7,  7, ISOSCELES);
  530.           COMPARE( 1,  1,  1, EQUILATERAL);
  531.           COMPARE( 0,  4,  4, NOT_A_TRIANGLE);
  532.           COMPARE( 4,  0,  4, NOT_A_TRIANGLE);
  533.           COMPARE( 4,  4,  0, NOT_A_TRIANGLE);
  534.           COMPARE( 0,  4,  3, NOT_A_TRIANGLE);
  535.           COMPARE( 3,  0,  4, NOT_A_TRIANGLE);
  536.           COMPARE( 4,  3,  0, NOT_A_TRIANGLE);
  537.           COMPARE(-1,  4,  4, NOT_A_TRIANGLE);
  538.           COMPARE( 4, -1,  4, NOT_A_TRIANGLE);
  539.           COMPARE( 4,  4, -1, NOT_A_TRIANGLE);
  540.           COMPARE(-1,  4,  3, NOT_A_TRIANGLE);
  541.           COMPARE( 3, -1,  4, NOT_A_TRIANGLE);
  542.           COMPARE( 4,  3, -1, NOT_A_TRIANGLE);
  543.           COMPARE( 2,  4,  6, NOT_A_TRIANGLE);
  544.           COMPARE( 1,  3,  2, NOT_A_TRIANGLE);
  545.           COMPARE( 3,  1,  2, NOT_A_TRIANGLE);
  546.           COMPARE( 1,  2,  4, NOT_A_TRIANGLE);
  547.           COMPARE( 1,  4,  2, NOT_A_TRIANGLE);
  548.           COMPARE( 4,  1,  2, NOT_A_TRIANGLE);
  549.           COMPARE( 0,  0,  0, NOT_A_TRIANGLE);
  550.           COMPARE( 0,  0,  4, NOT_A_TRIANGLE);
  551.           COMPARE( 0,  4,  0, NOT_A_TRIANGLE);
  552.           COMPARE( 4,  0,  0, NOT_A_TRIANGLE);
  553.           COMPARE( 3,  3,  7, NOT_A_TRIANGLE);
  554.           COMPARE( 3,  7,  3, NOT_A_TRIANGLE);
  555.           COMPARE( 6,  3,  3, NOT_A_TRIANGLE);
  556.           COMPARE(-3, -4, -5, NOT_A_TRIANGLE);
  557.           if PASSED then
  558.              PUT_LINE("Congratulations, you completed the assignment!");
  559.           end if;
  560.        end TRITEST;
  561.                                   -- continued --
  562.  
  563.                                                                       Page 10
  564.  
  565.        separate(TRITEST)
  566.        procedure COMPARE(A, B, C: in INTEGER; RIGHT_ANSWER : in TRIANGLE) is
  567.           package INT_IO is new INTEGER_IO(INTEGER); use INT_IO;
  568.           package TRI_IO is new ENUMERATION_IO(TRIANGLE); use TRI_IO;
  569.           MY_ANSWER : TRIANGLE := TRITYPE(A, B, C);
  570.        begin
  571.           if MY_ANSWER /= RIGHT_ANSWER then
  572.              PUT("Sides:");
  573.              PUT(A, WIDTH => 3);
  574.              PUT(B, WIDTH => 3);
  575.              PUT(C, WIDTH => 3);
  576.              PUT("   My answer: ");
  577.              PUT(MY_ANSWER, WIDTH => 14);
  578.              PUT("   Right answer: ");
  579.              PUT(RIGHT_ANSWER);
  580.              NEW_LINE;
  581.              PASSED := FALSE;
  582.           end if;
  583.        end COMPARE;
  584.  
  585.                                                                       Page 11
  586.  
  587.            STEPS FOR OUTSIDE ASSIGNMENT 2, EXERCISE IN ENUMERATION TYPES
  588.  
  589.        1.  Compile the test driver TRITEST.ADA.  Also, make a copy of the
  590.            dummy solution by typing COPY TRITYPE.DUM TRITYPE.ADA.  You need
  591.            do this step only once.
  592.  
  593.        2.  Edit TRITYPE.ADA to become your real solution.  You can skip this
  594.            step the first time through, to see error messages from the test
  595.            driver.
  596.  
  597.        3.  Compile your solution TRITYPE.ADA.  If there are compiler errors,
  598.            go back to step 2.
  599.  
  600.        4.  Link with the name of the main program TRITEST.  Then execute.  If
  601.            the test driver prints error messages, go back to step 2.
  602.  
  603.        5.  When the message "Congratulations, you completed the assignment!"
  604.            is printed, you'll have a chance to compare your solution with
  605.            ours.
  606.  
  607.                                                                       Page 12
  608.  
  609.        NEXTDATE.ADA
  610.        ------------
  611.  
  612.        with TEXT_IO; use TEXT_IO;
  613.        procedure NEXTDATE is
  614.           type MONTH_TYPE is
  615.              (JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC);
  616.           subtype DAY_SUBTYPE is INTEGER range 1 .. 31;
  617.           type DATE is
  618.              record
  619.                 DAY   : DAY_SUBTYPE;
  620.                 MONTH : MONTH_TYPE;
  621.                 YEAR  : POSITIVE;
  622.              end record;
  623.           PASSED : BOOLEAN := TRUE;
  624.           function TOMORROW(TODAY : in DATE) return DATE is separate;
  625.  
  626.           procedure DISPLAY (S : in STRING; D : in DATE) is
  627.              package INT_IO is new INTEGER_IO(INTEGER); use INT_IO;
  628.              package MON_IO is new ENUMERATION_IO(MONTH_TYPE); use MON_IO;
  629.           begin
  630.              PUT(S);
  631.              PUT(D.DAY, WIDTH => 3);  PUT(" ");
  632.              PUT(D.MONTH);
  633.              PUT(D.YEAR, WIDTH => 5);  NEW_LINE;
  634.           end DISPLAY;
  635.           procedure COMPARE(TODAY, RIGHT_ANSWER : in DATE) is
  636.              MY_ANSWER : DATE := TOMORROW(TODAY);
  637.           begin
  638.              if MY_ANSWER /= RIGHT_ANSWER then
  639.                 DISPLAY("Today:       ", TODAY);
  640.                 DISPLAY("My answer:   ", MY_ANSWER);
  641.                 display("Right answer:", RIGHT_ANSWER);
  642.                 NEW_LINE;
  643.                 PASSED := FALSE;
  644.              end if;
  645.           end COMPARE;
  646.        begin
  647.           COMPARE((12,DEC,1815), (13,DEC,1815)); -- ordinary date
  648.           COMPARE(( 3,FEB,1986), ( 4,FEB,1986)); -- ordinary date in Feb.
  649.           COMPARE((30,JUN,1981), ( 1,JUL,1981)); -- last day of 30-day month
  650.           COMPARE((30,SEP,3999), ( 1,OCT,3999)); -- last day of 30-day month
  651.           COMPARE((31,MAR,1876), ( 1,APR,1876)); -- last day of 31-day month
  652.           COMPARE((31,AUG,1984), ( 1,SEP,1984)); -- last day of 31-day month
  653.           COMPARE((31,DEC,1966), ( 1,JAN,1967)); -- last day of year
  654.           COMPARE((28,FEB,1980), (29,FEB,1980)); -- leap year
  655.           COMPARE((28,FEB,1600), (29,FEB,1600)); -- century leap year
  656.           COMPARE((28,FEB,2100), ( 1,MAR,2100)); -- century non-leap year
  657.           COMPARE((28,FEB,1982), ( 1,MAR,1982)); -- non-leap year
  658.           COMPARE((29,FEB,1980), ( 1,MAR,1980)); -- leap day in leap year
  659.           if PASSED then
  660.              PUT_LINE("Congratulations, you completed the assignment!");
  661.           end if;
  662.        end NEXTDATE;
  663.  
  664.                                                                       Page 13
  665.  
  666.                 STEPS FOR OUTSIDE ASSIGNMENT 3, EXERCISE IN RECORDS
  667.  
  668.        1.  Compile the test driver NEXTDATE.ADA.  Also, make a copy of the
  669.            dummy solution by typing COPY TOMORROW.DUM TOMORROW.ADA.  You need
  670.            do this step only once.
  671.  
  672.        2.  Edit TOMORROW.ADA to become your real solution.  You can skip this
  673.            step the first time through, to see error messages from the test
  674.            driver.
  675.  
  676.        3.  Compile TOMORROW.ADA.  If there are compiler errors, go back to
  677.            step 2.
  678.  
  679.        4.  Link with the name of the main program NEXTDATE.  Then execute.
  680.            If the test driver prints error messages, go back to step 2.
  681.  
  682.        5.  When the message "Congratulations, you completed the assignment!"
  683.            is printed, you'll have a chance to compare your solution with
  684.            ours.
  685.  
  686.                                                                       Page 14
  687.  
  688.        FIBTEST.ADA
  689.        -----------
  690.  
  691.        with TEXT_IO; use TEXT_IO;
  692.        procedure FIBTEST is
  693.           PASSED : BOOLEAN := TRUE;
  694.           function FIBONACCI(N : in POSITIVE)return POSITIVE is separate;
  695.           procedure COMPARE (N : in POSITIVE; RIGHT_ANSWER : in POSITIVE) is
  696.              package INT_IO is new INTEGER_IO(INTEGER); use INT_IO;
  697.              MY_ANSWER : POSITIVE := FIBONACCI(N);
  698.           begin
  699.              if MY_ANSWER /= RIGHT_ANSWER then
  700.                 PUT("N:");  PUT(N);
  701.                 PUT("          My answer:");  PUT(MY_ANSWER);
  702.                 PUT("          Right answer:");  PUT(RIGHT_ANSWER);
  703.                 NEW_LINE;
  704.                 PASSED := FALSE;
  705.              end if;
  706.           end COMPARE;
  707.        begin
  708.           COMPARE(1,1);
  709.           COMPARE(2,1);
  710.           COMPARE(3,2);
  711.           COMPARE(4,3);
  712.           COMPARE(5,5);
  713.           COMPARE(6,8);
  714.           COMPARE(7,13);
  715.           COMPARE(10,55);
  716.           COMPARE(15,610);
  717.           COMPARE(20,6765);
  718.           if PASSED then
  719.              PUT_LINE("Congratulations, you completed the assignment!");
  720.           end if;
  721.        end FIBTEST;
  722.  
  723.                                                                       Page 15
  724.  
  725.                STEPS FOR OUTSIDE ASSIGNMENT 4, EXERCISE IN RECURSION
  726.  
  727.        1.  Compile the test driver FIBTEST.ADA.  Also, make a copy of the
  728.            dummy solution by typing COPY FIB.DUM FIB.ADA.  You need do this
  729.            step only once.
  730.  
  731.        2.  Edit FIB.ADA to become your real solution.  You can skip this step
  732.            the first time through, to see error messages from the test
  733.            driver.
  734.  
  735.        3.  Compile FIB.ADA.  If there are compiler errors, go back to step 2.
  736.  
  737.        4.  Link with the name of the main program FIBTEST.  Then execute.  If
  738.            the test driver prints error messages, go back to step 2.
  739.  
  740.        5.  When the message "Congratulations, you completed the assignment!"
  741.            is printed, you'll have a chance to compare your solution with
  742.            ours.
  743.  
  744.                                                                       Page 16
  745.  
  746.        SIMPLIFIED SPECIFICATION FOR TEXT_IO
  747.        ------------------------------------
  748.  
  749.        package TEXT_IO is
  750.           type FILE_TYPE is limited private;
  751.           type FILE_MODE is (IN_FILE, OUT_FILE);
  752.           type COUNT is ... (a user-defined type similar to INTEGER);
  753.           STATUS_ERROR, MODE_ERROR, NAME_ERROR, END_ERROR : exception;
  754.  
  755.           procedure CREATE      (FILE : in out FILE_TYPE;
  756.                                  MODE : in FILE_MODE := OUT_FILE;
  757.                                  NAME : in STRING);
  758.  
  759.           procedure OPEN        (FILE : in out FILE_TYPE;
  760.                                  MODE : in FILE_MODE;
  761.                                  NAME : in STRING);
  762.  
  763.           procedure CLOSE       (FILE : in out FILE_TYPE);
  764.           procedure DELETE      (FILE : in out FILE_TYPE);
  765.           procedure NEW_LINE    (SPACING : in COUNT := 1);
  766.           procedure NEW_LINE    (FILE : in FILE_TYPE;
  767.                                  SPACING : in COUNT := 1);
  768.           procedure SKIP_LINE   (SPACING : in COUNT := 1);
  769.           procedure SKIP_LINE   (FILE : in FILE_TYPE;
  770.                                  SPACING : in COUNT := 1);
  771.           function  END_OF_FILE (FILE : in FILE_TYPE) return BOOLEAN;
  772.           procedure PUT         (ITEM : in CHARACTER);
  773.           procedure PUT         (FILE : in FILE_TYPE; ITEM : in CHARACTER);
  774.           procedure GET         (ITEM : out CHARACTER);
  775.           procedure GET         (FILE : in FILE_TYPE; ITEM : out CHARACTER);
  776.           procedure PUT         (ITEM : in STRING);
  777.           procedure PUT         (FILE : in FILE_TYPE; ITEM : in STRING);
  778.           procedure PUT_LINE    (ITEM : in STRING);
  779.           procedure PUT_LINE    (FILE : in FILE_TYPE; ITEM : in STRING);
  780.           procedure GET_LINE    (ITEM : out STRING; LAST : out NATURAL);
  781.           procedure GET_LINE    (FILE : in FILE_TYPE;
  782.                                  ITEM : out STRING;
  783.                                  LAST : out NATURAL);
  784.           generic
  785.              type NUM is range <>;
  786.           package INTEGER_IO is
  787.              procedure GET (ITEM : out NUM);
  788.              procedure GET (FILE : in FILE_TYPE; ITEM : out NUM);
  789.              procedure PUT (ITEM  : in NUM;
  790.                             WIDTH : in INTEGER := ...;
  791.                             BASE  : in INTEGER := 10);
  792.              procedure PUT (FILE  : in FILE_TYPE;
  793.                             ITEM  : in NUM;
  794.                             WIDTH : in INTEGER := ...;
  795.                             BASE  : in INTEGER := 10);
  796.           end INTEGER_IO;
  797.  
  798.                                   -- continued --
  799.  
  800.                                                                       Page 17
  801.  
  802.           generic
  803.              type NUM is digits <>;
  804.           package FLOAT_IO is
  805.              procedure GET (ITEM : out NUM);
  806.              procedure GET (FILE : in FILE_TYPE; ITEM : out NUM);
  807.              procedure PUT (ITEM : in NUM;
  808.                             FORE : in INTEGER := 2;
  809.                             AFT  : in INTEGER := ...;
  810.                             EXP  : in INTEGER := 3);
  811.              procedure PUT (FILE : in FILE_TYPE;
  812.                             ITEM : in NUM;
  813.                             FORE : in INTEGER := 2;
  814.                             AFT  : in INTEGER := ...;
  815.                             EXP  : in INTEGER := 3);
  816.           end FLOAT_IO;
  817.  
  818.           generic
  819.              type ENUM is (<>);
  820.           package ENUMERATION_IO is
  821.              procedure GET (ITEM : out ENUM);
  822.              procedure GET (FILE : in FILE_TYPE; ITEM : out ENUM);
  823.              procedure PUT (ITEM : in ENUM; WIDTH : in INTEGER := 0);
  824.              procedure PUT (FILE : in FILE_TYPE;
  825.                             ITEM : in ENUM; WIDTH : in INTEGER := 0);
  826.           end ENUMERATION_IO;
  827.  
  828.        private
  829.  
  830.           type FILE_TYPE is ... (implementation dependent);
  831.  
  832.        end TEXT_IO;
  833.  
  834.                                                                       Page 18
  835.  
  836.        Program to copy a simple text file:
  837.        -----------------------------------
  838.  
  839.        with TEXT_IO; use TEXT_IO;
  840.        procedure COPY is
  841.           F1, F2 : FILE_TYPE;
  842.           S      : STRING(1 .. 80);
  843.           LEN    : INTEGER;
  844.        begin
  845.           PUT("Input file: ");  GET_LINE(S, LEN);
  846.           OPEN(FILE => F1, MODE => IN_FILE, NAME => S(1 .. LEN));
  847.           PUT("Output file: ");  GET_LINE(S, LEN);
  848.           CREATE(FILE => F2, MODE => OUT_FILE, NAME => S(1 .. LEN));
  849.           while not END_OF_FILE(F1) loop
  850.              GET_LINE(F1, S, LEN);
  851.              PUT_LINE(F2, S(1 .. LEN));
  852.           end loop;
  853.           CLOSE(F1);
  854.           CLOSE(F2);
  855.        end COPY;
  856.  
  857.  
  858.  
  859.        Function to test if a text file exists:
  860.        ---------------------------------------
  861.  
  862.        with TEXT_IO; use TEXT_IO;
  863.        function EXISTS(FILE_NAME : in STRING) return BOOLEAN is
  864.           F      : FILE_TYPE;
  865.           ANSWER : BOOLEAN := TRUE;
  866.        begin
  867.           begin
  868.              OPEN(F, IN_FILE, S);
  869.              CLOSE(F);
  870.           exception
  871.              when NAME_ERROR => ANSWER := FALSE;
  872.           end;
  873.           return ANSWER;
  874.        end EXISTS;
  875.  
  876.                                                                       Page 19
  877.  
  878.                         REQUIREMENTS FOR THE PROGRAM LEDIT
  879.  
  880.        This assignment will give you practice in writing a program of greater
  881.        complexity than the previous programs you've written.  Imagine that
  882.        your screen editor is unavailable to a particular user, perhaps
  883.        because he's dialing your computer from a remote location, and your
  884.        screen editor writes directly to the screen.  You want to write a very
  885.        simple line editor, LEDIT, that could be used in such circumstances.
  886.        While your computer already has a line editor called EDLIN, it's
  887.        difficult to learn to use.  LEDIT will take almost no effort to learn.
  888.        The only commands are LIST and EXIT.  The line editor edits by means
  889.        of line numbers, similar to the Basic language.
  890.  
  891.        The user begins each line of text that he types with a line number
  892.        from 1 to 29999.  Line numbers must be integers.  The upper limit
  893.        29999 was chosen so that the simple type INTEGER could be used - in
  894.        any implementation of Ada.   Regardless of the order in which lines
  895.        are typed, LEDIT maintains a linked list of lines in order by number.
  896.        Also, line numbers may be preceded by any number of spaces.  For
  897.        example, if the user types
  898.  
  899.        40 -- This is a comment.
  900.           20 begin
  901.        10 with TEXT_IO; use TEXT_IO;
  902.              30 end ADD;
  903.  
  904.        and then types LIST, the editor will type
  905.  
  906.           10 with TEXT_IO; use TEXT_IO;
  907.           20 begin
  908.           30 end ADD;
  909.           40 -- This is a comment.
  910.  
  911.        To INSERT lines, the user merely types lines with intermediate line
  912.        numbers.  For example, if he types
  913.  
  914.        15 procedure HELLO is
  915.  
  916.        and then types LIST, LEDIT will type
  917.  
  918.           10 with TEXT_IO; use TEXT_IO;
  919.           15 procedure HELLO is
  920.           20 begin
  921.           30 end ADD;
  922.           40 -- This is a comment.
  923.  
  924.        To REPLACE an existing line, the user merely types a line with the
  925.        same line number as the line to be replaced.  For example, if he typed
  926.  
  927.        15 procedure ADD is
  928.        LIST
  929.  
  930.  
  931.                                   -- continued --
  932.  
  933.                                                                       Page 20
  934.  
  935.        LEDIT would then show
  936.  
  937.           10 with TEXT_IO; use TEXT_IO;
  938.           15 procedure ADD is
  939.           20 begin
  940.           30 end ADD;
  941.           40 -- This is a comment.
  942.  
  943.        Finally, to DELETE a line, the user merely types the number of the
  944.        line to be deleted, followed immediately by a carriage return.  Typing
  945.  
  946.        40
  947.        LIST
  948.  
  949.        would then produce
  950.  
  951.           10 with TEXT_IO; use TEXT_IO;
  952.           15 procedure ADD is
  953.           20 begin
  954.           30 end ADD;
  955.  
  956.        Thus the user can INSERT, REPLACE, and DELETE lines, all by line
  957.        numbers, without learning any commands.  Note that in this simple
  958.        editor there is no "cursor" or "current line."
  959.  
  960.        The space is not required after the line number.  These two lines have
  961.        exactly the same effect:
  962.  
  963.        20 begin
  964.        20begin
  965.  
  966.        Of course, if the text of the line begins with a digit, a space will
  967.        be required to separate it from the line number.  In any event, LEDIT
  968.        always leaves one blank space after the line number when LISTing, for
  969.        readability.  It always allows exactly five spaces for the line number
  970.        itself.
  971.  
  972.        However, any EXTRA spaces typed after the line number are significant.
  973.        The three lines below each contain three EXTRA spaces after the line
  974.        number, for a total of four spaces.
  975.  
  976.        24    PUT(2 + 2);
  977.        26    NEW_LINE;
  978.        18    package MY_INT_IO is new INTEGER_IO(INTEGER); use MY_INT_IO;
  979.  
  980.        They have the effect of indenting the text three spaces.  LIST now
  981.        shows
  982.  
  983.  
  984.                                   -- continued --
  985.  
  986.                                                                       Page 21
  987.  
  988.           10 with TEXT_IO; use TEXT_IO;
  989.           15 procedure ADD is
  990.           18    package MY_INT_IO is new INTEGER_IO(INTEGER); use MY_INT_IO;
  991.           20 begin
  992.           24    PUT(2 + 2);
  993.           26    NEW_LINE;
  994.           30 end ADD;
  995.  
  996.        Although typing a line number followed immediately by a carriage
  997.        return deletes a line (if there is a line by that number), typing a
  998.        line number followed by a single space causes an empty line to be
  999.        introduced into the file.  For example, typing 12 followed by a single
  1000.        space and a carriage return would then cause LIST to type
  1001.  
  1002.           10 with TEXT_IO; use TEXT_IO;
  1003.           12
  1004.           15 procedure ADD is
  1005.           18    package MY_INT_IO is new INTEGER_IO(INTEGER); use MY_INT_IO;
  1006.           20 begin
  1007.           24    PUT(2 + 2);
  1008.           26    NEW_LINE;
  1009.           30 end ADD;
  1010.  
  1011.        When LEDIT is run, it prompts for the names of the input and output
  1012.        files.  If the input file exists, LEDIT prints "File found" and reads
  1013.        the file into its linked list, assigning line numbers starting with 10
  1014.        and incrementing by 10.  If the file does not exist, LEDIT prints
  1015.        "File not found," and the linked list is initially empty.  In any
  1016.        event, LEDIT creates an output file.  When the EXIT command is given,
  1017.        LEDIT writes the contents of the linked list to the output file.  In
  1018.        doing so, LEDIT removes the line numbers and the first blank after
  1019.        each line number.
  1020.  
  1021.        In the example above, let us assume that the user typed ADD.ADA for an
  1022.        output file name.  When he types EXIT, the new file ADD.ADA created by
  1023.        LEDIT will contain:
  1024.  
  1025.        with TEXT_IO; use TEXT_IO;
  1026.  
  1027.        procedure ADD is
  1028.           package MY_INT_IO is new INTEGER_IO(INTEGER); use MY_INT_IO;
  1029.        begin
  1030.           PUT(2 + 2);
  1031.           NEW_LINE;
  1032.        end ADD;
  1033.  
  1034.        (Note that the file contains one empty line.)  Your program is not
  1035.        allowed to add any trailing blanks of its own in the output file.  If
  1036.        the user again runs LEDIT and specifies ADD.ADA as an input file, the
  1037.        editor will type "File found" and read the file.  LIST will show
  1038.  
  1039.  
  1040.                                   -- continued --
  1041.  
  1042.                                                                       Page 22
  1043.  
  1044.           10 with TEXT_IO; use TEXT_IO;
  1045.           20
  1046.           30 procedure ADD is
  1047.           40    package MY_INT_IO is new INTEGER_IO(INTEGER); use MY_INT_IO;
  1048.           50 begin
  1049.           60    PUT(2 + 2);
  1050.           70    NEW_LINE;
  1051.           80 end ADD;
  1052.  
  1053.        When he EXITs, the new file will contain modified text.  The old file
  1054.        will still be present until it is deleted.
  1055.  
  1056.        The two commands LIST and EXIT must be accepted in either upper or
  1057.        lower case, but for simplicity, they need not be accepted in a mixture
  1058.        of cases.  They may be preceded by any number of spaces.  The LIST
  1059.        command must be accepted in any of the following forms:
  1060.  
  1061.        LIST            (Lists all lines, if any, otherwise does nothing.)
  1062.        list 30         (Lists line 30, if it exists.)
  1063.        list 25 - 35    (Lists all lines between 25 and 35 inclusive, if any.)
  1064.        LIST - 35       (Lists all lines numbered 35 or less, if any.)
  1065.        list 25 -       (Lists all lines numbered 25 or more, if any.)
  1066.  
  1067.        Furthermore, all spaces are optional in the LIST command provided that
  1068.        LIST is written solid, so that LIST25-35 is equivalent to the third
  1069.        example above.  Any other forms of LIST should be flagged as an error.
  1070.        In particular, all of these should give error messages:
  1071.  
  1072.        LIST -
  1073.        list 2A
  1074.        LIST 30 - 50 -
  1075.        list 30 - -50
  1076.        list 30-A
  1077.        LIST XYZ
  1078.  
  1079.        The EXIT command must stand alone; unlike LIST, EXIT is never followed
  1080.        by anything.  Except for the LIST and EXIT commands, every line typed
  1081.        must begin with a number between 1 and 29999.  (Of course, the user
  1082.        should be able to type just a carriage return with no effect.)  LEDIT
  1083.        should check that line numbers are in range when adding or replacing
  1084.        lines.  The LIST command need not check the range of the line numbers,
  1085.        because it should be impossible to create lines with improper numbers.
  1086.        Your program need not handle input files so long that 29999 would be
  1087.        exceeded when it assigns line numbers starting at 10 with an increment
  1088.        of 10.
  1089.  
  1090.        Your LEDIT must prompt for input and output file names at the start,
  1091.        and it may print a prompt for each line that the user types.  You may
  1092.        assume some maximum length for an input line (e.g., 80), and assume
  1093.        that no file will have lines longer than the maximum.
  1094.  
  1095.  
  1096.                                   -- continued --
  1097.  
  1098.                                                                       Page 23
  1099.  
  1100.        For simplicity, there's no way to edit a line except by retyping it.
  1101.        Also, there's no way to copy or move a line or a block of lines, and
  1102.        no way to delete a block of lines except one line at a time.  There's
  1103.        no RENUMBER command.  The user can renumber the entire file starting
  1104.        at 10 with an increment of 10 by EXITing and then rerunning LEDIT.
  1105.        There's no AUTO command to make the editor type the line numbers
  1106.        automatically while text is being typed from the keyboard, and there's
  1107.        no means of recovering from a system crash that occurred during an
  1108.        edit.  Additionally, there's no way to search for a string, or replace
  1109.        one string with another.  These features would all be desirable, but
  1110.        we feel that Outside Assignment 5 is challenging as it stands.
  1111.  
  1112.        You're encouraged to make use of Ada's separate compilation feature,
  1113.        so that you don't have to recompile the entire program when developing
  1114.        a module.
  1115.  
  1116.        As a point of reference, our solution to this assignment consists of
  1117.        about 180 lines of code on four pages.  If you have any questions
  1118.        about what LEDIT should do, you can compile and run our solution,
  1119.        which is in LEDIT.ANS.
  1120.  
  1121.        The following declarations are offered by way of suggestion only, and
  1122.        you should use them only if you feel comfortable with them:
  1123.  
  1124.             MAX_LENGTH : constant INTEGER := 80;
  1125.             type TEXT is
  1126.                record
  1127.                   LEN : INTEGER range 0 .. MAX_LENGTH;
  1128.                   VAL : STRING(1 .. MAX_LENGTH);
  1129.                end record;
  1130.  
  1131.             type LINK;
  1132.             type P is access LINK;
  1133.             type LINK is
  1134.                record
  1135.                   NUM  : POSITIVE;
  1136.                   LINE : TEXT;
  1137.                   NEXT : P;
  1138.                end record;
  1139.  
  1140.             function STR(T : in TEXT) return STRING;
  1141.  
  1142.        In our solution, we used type TEXT as above.  However, we didn't write
  1143.        a TEXT_HANDLER package, because it didn't seem to be needed.  We used
  1144.        a one-page main program with several "separate" subprograms.
  1145.  
  1146.                                                                       Page 24
  1147.  
  1148.            STEPS FOR OUTSIDE ASSIGNMENT 5, WRITING A SIMPLE LINE EDITOR
  1149.  
  1150.        1.  Carefully read the requirements starting on page 19 of these
  1151.            notes.  Take your time.
  1152.  
  1153.        2.  Write the Ada code, compile, and link.  Call the main program
  1154.            LEDIT.  If you have any questions about what LEDIT should do, you
  1155.            can compile and run our solution, which is in LEDIT.ANS.
  1156.  
  1157.        3.  Refer to pages 25-27 of these notes for instructions on testing
  1158.            your line editor.  If any tests are failed, go back to step 2.
  1159.  
  1160.        4.  When all the tests are passed, you've completed the assignment and
  1161.            will have a chance to compare your solution with ours.
  1162.  
  1163.                                                                       Page 25
  1164.  
  1165.                                  HOW TO TEST LEDIT
  1166.  
  1167.        1.  Run LEDIT and give the name of an input file that doesn't exist.
  1168.        LEDIT should say "File not found."  Give another name of a file that
  1169.        doesn't exist for an output file.
  1170.  
  1171.        2.  Type "LIST" (without the quotes) to make sure that LEDIT can
  1172.        handle the LIST command when no text has been entered.
  1173.  
  1174.        3.  Type "EXI".  The program should NOT exit.  If it exits, it's
  1175.        probably because the "T" was left in the input buffer from the LIST
  1176.        command.  Your program is checking the first four characters of the
  1177.        input buffer without checking the length of the typed command.
  1178.  
  1179.        4.  Type "ABC".  The program should print a message about an illegal
  1180.        or unrecognized command, or syntax error.
  1181.  
  1182.        5.  Type "0X" (zero followed by X).  The program should reject this
  1183.        line, printing a message about an invalid line number.  Type "0 X".
  1184.        The same thing should happen.  Try "30000X", then "30000 X", and then
  1185.        "3000000000 X".  The program should reject these, as well.
  1186.  
  1187.        6.  Type "-1 X" and "-1X".  The program should reject these lines,
  1188.        printing a message either about an unrecognized command (or syntax
  1189.        error), or an invalid line number.
  1190.  
  1191.        7.  Type a simple carriage return.  There should be no effect, except
  1192.        for any prompt being repeated.
  1193.  
  1194.        8.  Type the following exactly as shown.  Note that lines 1000 and 100
  1195.        each contain four spaces:
  1196.  
  1197.        30 X
  1198.        1 The
  1199.        29999 Z
  1200.        1000    used
  1201.        100    is
  1202.        10000 to test
  1203.           30file --
  1204.        1 This
  1205.            29999 LEDIT.
  1206.  
  1207.        9.  Type "list".  You should see the following, lined up exactly as
  1208.        shown.  There should be exactly five spaces for the line numbers.
  1209.  
  1210.            1 This
  1211.           30 file --
  1212.          100    is
  1213.         1000    used
  1214.        10000 to test
  1215.        29999 LEDIT.
  1216.  
  1217.  
  1218.                                   -- continued --
  1219.  
  1220.                                                                       Page 26
  1221.  
  1222.        10.  Try a line of text beginning with a number: "20000 123 45".  Then
  1223.        type "   LIST" with three leading spaces.  You should see
  1224.  
  1225.            1 This
  1226.           30 file --
  1227.          100    is
  1228.         1000    used
  1229.        10000 to test
  1230.        20000 123 45
  1231.        29999 LEDIT.
  1232.  
  1233.        11.  Insert an empty line by typing "15000 ".  Then type "LIST".  You
  1234.        should see
  1235.  
  1236.            1 This
  1237.           30 file --
  1238.          100    is
  1239.         1000    used
  1240.        10000 to test
  1241.        15000
  1242.        20000 123 45
  1243.        29999 LEDIT.
  1244.  
  1245.        12.  Type "EXIT ABC".  LEDIT should print a error message and NOT
  1246.        exit.  The requirements say that the EXIT command must stand alone.
  1247.  
  1248.        13.  Type "   exit".  LEDIT should exit, and you should have a new
  1249.        file with the output file name you gave in step 1.  Type the file with
  1250.        the TYPE command.  You should see exactly this, starting in column 1:
  1251.  
  1252.        This
  1253.        file --
  1254.           is
  1255.           used
  1256.        to test
  1257.  
  1258.        123 45
  1259.        LEDIT.
  1260.  
  1261.        14.  Run LEDIT again, using for an input file the name of the OUTPUT
  1262.        file you specified in step 1.  This time the program should say "File
  1263.        found."  Choose yet another name for an output file for this step.
  1264.        Type "list".  You should see the following, exactly as shown:
  1265.  
  1266.           10 This
  1267.           20 file --
  1268.           30    is
  1269.           40    used
  1270.           50 to test
  1271.           60
  1272.           70 123 45
  1273.           80 LEDIT.
  1274.  
  1275.                                   -- continued --
  1276.  
  1277.                                                                       Page 27
  1278.  
  1279.        15.  Type "  LIST 30".  You should see only line 30.  Type
  1280.        "list25-45".  You should see only lines 30 and 40.  Type "LIST  - 40".
  1281.        You should see lines 10 through 40.  Type "list35 -".  You should see
  1282.        lines 40 through 80.  Type "list 15".  The program should either do
  1283.        nothing or print a message that line 15 doesn't exist.  Now type
  1284.        "list" again and check that the result is the same as in step 14.
  1285.  
  1286.        16.  Try each of the following.  In each case, LEDIT should print an
  1287.        error message:
  1288.  
  1289.        LIST -
  1290.        list 2A
  1291.        list 30 - 50 -
  1292.        list 30 - -50
  1293.           list 30-A
  1294.        LIST XYZ
  1295.  
  1296.        17.  Type "70" to delete line 70.  Type "LIST".  You should see
  1297.  
  1298.           10 This
  1299.           20 file --
  1300.           30    is
  1301.           40    used
  1302.           50 to test
  1303.           60
  1304.           80 LEDIT.
  1305.  
  1306.        18.  Delete line 60.  Do not list.  Now delete line 80 and list.  You
  1307.        should see
  1308.  
  1309.           10 This
  1310.           20 file --
  1311.           30    is
  1312.           40    used
  1313.           50 to test
  1314.  
  1315.        19.  Delete line 10 and list.  You should see lines 20 through 50
  1316.        above.  Delete line 50 and list.  You should see lines 20 through 40.
  1317.        Delete line 30 and list.  You should see lines 20 and 40.  Delete line
  1318.        40 and list.  You should see only line 20.  Delete line 20 and list.
  1319.        The result should be the same as in step 2.  Type "EXIT".
  1320.  
  1321.        20.  If your program passed all these tests, sincere congratulations!
  1322.        You've completed a difficult assignment.  We hope that you feel
  1323.        comfortable with Ada now.  If you like, you can compare your solution
  1324.        with ours, starting on page 28 of these notes.  When you go back to
  1325.        ADA-TUTR, you'll learn to write your own generic packages, procedures,
  1326.        and functions.
  1327.  
  1328.                                                                       Page 28
  1329.  
  1330.        LEDIT.ANS
  1331.        ---------
  1332.  
  1333.        -- Our solution to Outside Assignment 5
  1334.        with TEXT_IO; use TEXT_IO;
  1335.        procedure LEDIT is
  1336.  
  1337.           MAX_LENGTH              : constant := 80;
  1338.           MAX_LINE_NUMBER         : constant := 29_999;
  1339.           type TEXT is
  1340.              record
  1341.                 LEN : INTEGER range 0 .. MAX_LENGTH := 0;
  1342.                 VAL : STRING(1 .. MAX_LENGTH);
  1343.              end record;
  1344.           type LINK;
  1345.           type P is access LINK;
  1346.           type LINK is
  1347.              record
  1348.                 NUM  : POSITIVE;
  1349.                 LINE : TEXT;
  1350.                 NEXT : P;
  1351.              end record;
  1352.           HEAD                    : P := new LINK;
  1353.           TEMP                    : P;
  1354.           INPUT_FILE, OUTPUT_FILE : FILE_TYPE;
  1355.           INPUT                   : TEXT;
  1356.           FINISHED                : BOOLEAN := FALSE;
  1357.           LINE_NUM                : NATURAL := 10;
  1358.  
  1359.           function  STR(T : in TEXT) return STRING is separate;
  1360.           procedure READ_INPUT_FILE is separate;
  1361.           procedure DO_COMMAND is separate;
  1362.        begin
  1363.           PUT("Input file: ");  GET_LINE(INPUT.VAL, INPUT.LEN);
  1364.           READ_INPUT_FILE;
  1365.  
  1366.           PUT("Output file: ");  GET_LINE(INPUT.VAL, INPUT.LEN);
  1367.           CREATE(OUTPUT_FILE, NAME => STR(INPUT));
  1368.  
  1369.           -- Get and process commands.
  1370.           while not FINISHED loop
  1371.              PUT("> ");  GET_LINE(INPUT.VAL, INPUT.LEN);
  1372.              DO_COMMAND;
  1373.           end loop;
  1374.  
  1375.           -- Write the output file.
  1376.           TEMP := HEAD.NEXT;     -- Skip unused link at start of linked list.
  1377.           while TEMP /= null loop
  1378.              PUT_LINE(OUTPUT_FILE, STR(TEMP.LINE));    -- Write line of text.
  1379.              TEMP := TEMP.NEXT;                             -- Get next link.
  1380.           end loop;
  1381.        end LEDIT;
  1382.  
  1383.                                   -- continued --
  1384.  
  1385.                                                                       Page 29
  1386.  
  1387.        separate(LEDIT)
  1388.        function STR(T : in TEXT) return STRING is
  1389.        begin
  1390.           return T.VAL(1 .. T.LEN);
  1391.        end STR;
  1392.  
  1393.        separate (LEDIT)
  1394.        procedure READ_INPUT_FILE is
  1395.        begin     -- If the input file exists, print a message and read it in.
  1396.           OPEN(INPUT_FILE, IN_FILE, STR(INPUT));
  1397.           PUT_LINE("File found.");
  1398.           TEMP := HEAD;
  1399.           while not END_OF_FILE(INPUT_FILE) loop
  1400.              GET_LINE(INPUT_FILE, INPUT.VAL, INPUT.LEN);      -- Read a line.
  1401.              TEMP.NEXT := new LINK'(LINE_NUM, INPUT, null);   -- Add to list.
  1402.              TEMP := TEMP.NEXT;              -- Advance pointer to next link.
  1403.              LINE_NUM := LINE_NUM + 10;
  1404.           end loop;
  1405.           CLOSE(INPUT_FILE);
  1406.        exception    -- If the input file doesn't exist, just print a message.
  1407.           when NAME_ERROR => PUT_LINE("File not found.");
  1408.        end READ_INPUT_FILE;
  1409.  
  1410.        separate(LEDIT)
  1411.        procedure DO_COMMAND is
  1412.           procedure DELETE_FIRST_CHARACTER(T : in out TEXT) is separate;
  1413.           procedure GET_LEADING_INTEGER(N : out NATURAL) is separate;
  1414.           procedure STRIP_LEADING_SPACES_FROM_INPUT is separate;
  1415.           procedure ADD_DELETE_REPLACE_LINE is separate;
  1416.           procedure LIST is separate;
  1417.        begin
  1418.           STRIP_LEADING_SPACES_FROM_INPUT;
  1419.           if STR(INPUT) = "exit" or STR(INPUT) = "EXIT" then
  1420.              FINISHED := TRUE;
  1421.           elsif INPUT.LEN >= 4 and (INPUT.VAL(1 .. 4) = "list" or
  1422.                                       INPUT.VAL(1 .. 4) = "LIST") then
  1423.              LIST;
  1424.           elsif INPUT.LEN > 0 and INPUT.VAL(1) not in '0' .. '9' then
  1425.              PUT_LINE("Unrecognized command.");
  1426.           elsif INPUT.LEN > 0 then
  1427.              GET_LEADING_INTEGER(LINE_NUM);
  1428.              if LINE_NUM not in 1 .. MAX_LINE_NUMBER then
  1429.                 PUT_LINE("Illegal line number.");
  1430.              else
  1431.                 ADD_DELETE_REPLACE_LINE;
  1432.              end if;
  1433.           end if;
  1434.        exception
  1435.           when NUMERIC_ERROR => PUT_LINE("Line number too large.");
  1436.        end DO_COMMAND;
  1437.  
  1438.  
  1439.                                   -- continued --
  1440.  
  1441.                                                                       Page 30
  1442.  
  1443.        separate(LEDIT.DO_COMMAND)
  1444.        procedure ADD_DELETE_REPLACE_LINE is
  1445.           INP : TEXT := INPUT;
  1446.        begin
  1447.           if INP.LEN > 0 and INP.VAL(1) = ' ' then  -- Treat "9x" like "9 x".
  1448.              DELETE_FIRST_CHARACTER(INP);
  1449.           end if;
  1450.           TEMP := HEAD;     -- Find where this number belongs in linked list.
  1451.           while TEMP /= null and then TEMP.NEXT /= null and then
  1452.                                       TEMP.NEXT.NUM <= LINE_NUM loop
  1453.              if TEMP.NEXT.NUM = LINE_NUM then
  1454.                 TEMP.NEXT := TEMP.NEXT.NEXT;                  -- Delete line.
  1455.              else
  1456.                 TEMP := TEMP.NEXT;           -- Advance to next link in list.
  1457.              end if;
  1458.           end loop;
  1459.           if INPUT.LEN > 0 then                                  -- Add line.
  1460.              TEMP.NEXT := new LINK'(LINE_NUM, INP, TEMP.NEXT);
  1461.           end if;
  1462.        end ADD_DELETE_REPLACE_LINE;
  1463.  
  1464.        separate(LEDIT.DO_COMMAND)
  1465.        procedure DELETE_FIRST_CHARACTER(T : in out TEXT) is
  1466.        begin
  1467.           T.VAL(1 .. MAX_LENGTH - 1) := T.VAL(2 .. MAX_LENGTH);
  1468.           T.LEN := T.LEN - 1;
  1469.        end DELETE_FIRST_CHARACTER;
  1470.  
  1471.        separate(LEDIT.DO_COMMAND)
  1472.        procedure GET_LEADING_INTEGER(N : out NATURAL) is
  1473.           ANS: INTEGER := 0;
  1474.        begin
  1475.           while INPUT.LEN > 0 and INPUT.VAL(1) in '0' .. '9' loop
  1476.              ANS := ANS*10 + CHARACTER'POS(INPUT.VAL(1)) -CHARACTER'POS('0');
  1477.              DELETE_FIRST_CHARACTER(INPUT);
  1478.           end loop;
  1479.           N := ANS;
  1480.        end GET_LEADING_INTEGER;
  1481.  
  1482.        separate(LEDIT.DO_COMMAND)
  1483.        procedure STRIP_LEADING_SPACES_FROM_INPUT is
  1484.        begin
  1485.           while INPUT.LEN > 0 and INPUT.VAL(1) = ' ' loop
  1486.              DELETE_FIRST_CHARACTER(INPUT);
  1487.           end loop;
  1488.        end STRIP_LEADING_SPACES_FROM_INPUT;
  1489.  
  1490.  
  1491.                                   -- continued --
  1492.  
  1493.                                                                       Page 31
  1494.  
  1495.        separate(LEDIT.DO_COMMAND)
  1496.        procedure LIST is
  1497.           package IIO is new INTEGER_IO(INTEGER); use IIO;
  1498.           START, FINISH : NATURAL;
  1499.           VALID         : BOOLEAN := TRUE;
  1500.        begin
  1501.           INPUT.LEN := INPUT.LEN - 4;      -- Delete the name of the command.
  1502.           INPUT.VAL(1 .. MAX_LENGTH - 4) := INPUT.VAL(5 .. MAX_LENGTH);
  1503.           STRIP_LEADING_SPACES_FROM_INPUT;
  1504.           if INPUT.LEN = 0 then          -- For "LIST" alone, list all lines.
  1505.              START := 0;
  1506.              FINISH := MAX_LINE_NUMBER + 1;
  1507.           else
  1508.              GET_LEADING_INTEGER(START);          -- Get number after "LIST".
  1509.              STRIP_LEADING_SPACES_FROM_INPUT;
  1510.              if INPUT.LEN = 0 then         -- For "LIST n", list only line n.
  1511.                 FINISH := START;
  1512.              elsif INPUT.VAL(1) /= '-' then        -- Else "-" must follow n.
  1513.                 VALID := FALSE;
  1514.              else
  1515.                 DELETE_FIRST_CHARACTER(INPUT);             -- Delete the "-".
  1516.                 STRIP_LEADING_SPACES_FROM_INPUT;
  1517.                 GET_LEADING_INTEGER(FINISH);         -- Get number after "-".
  1518.                 STRIP_LEADING_SPACES_FROM_INPUT;
  1519.                 if FINISH = 0 and START = 0 then     -- "LIST -" isn't valid.
  1520.                    VALID := FALSE;
  1521.                 elsif FINISH = 0 then  -- For "LIST n -", list n through end.
  1522.                    FINISH := MAX_LINE_NUMBER + 1;
  1523.                 end if;
  1524.                 VALID := VALID  and  INPUT.LEN = 0;   -- No trailing garbage.
  1525.              end if;
  1526.           end if;
  1527.           if not VALID then
  1528.              PUT_LINE("Illegal syntax for LIST.");
  1529.           else
  1530.              TEMP := HEAD.NEXT;  -- Skip unused link at start of linked list.
  1531.              while TEMP /= null and then TEMP.NUM <= FINISH loop
  1532.                 if TEMP.NUM >= START then
  1533.                    PUT(TEMP.NUM, WIDTH => 5);  -- Print line number, width 5.
  1534.                    PUT_LINE(' ' & STR(TEMP.LINE));     -- Print text of line.
  1535.                 end if;
  1536.                 TEMP := TEMP.NEXT;                          -- Get next link.
  1537.              end loop;
  1538.           end if;
  1539.        exception
  1540.           when NUMERIC_ERROR => PUT_LINE("Line number too large in LIST.");
  1541.        end LIST;
  1542.  
  1543.                                                                       Page 32
  1544.  
  1545.        TASKING.DUM
  1546.        -----------
  1547.  
  1548.        with text_io, calendar; use text_io, calendar;
  1549.        procedure tasking is
  1550.           interval        : constant duration := 5.0;
  1551.           total_intervals : constant positive := 9;
  1552.           start_time      : constant time := clock;
  1553.           quitting_time   : constant time := start_time +
  1554.                                                     total_intervals*interval;
  1555.           next_time       : time := start_time;
  1556.           task type tick is
  1557.              entry make_noise;
  1558.              entry shutdown;
  1559.           end tick;
  1560.           t               : tick;
  1561.           task body tick is
  1562.              quit : boolean := false;
  1563.           begin
  1564.              while not quit loop
  1565.                 select
  1566.                    accept make_noise do
  1567.                       put_line("Tick!");
  1568.                    end make_noise;
  1569.                 or
  1570.                    accept shutdown;
  1571.                    quit := true;
  1572.                 end select;
  1573.              end loop;
  1574.           end tick;
  1575.        begin
  1576.           while next_time < quitting_time loop
  1577.              t.make_noise;
  1578.              next_time := next_time + interval;
  1579.              delay next_time - clock; new_line;
  1580.           end loop;
  1581.           t.shutdown;
  1582.        end tasking;
  1583.  
  1584.                                                                       Page 33
  1585.  
  1586.                 STEPS FOR OUTSIDE ASSIGNMENT 6, EXERCISE IN TASKING
  1587.  
  1588.        1.  Make a copy of TASKING.DUM by typing COPY TASKING.DUM TASKING.ADA.
  1589.            Compile, link, and execute the program to make sure it prints
  1590.            "Tick!" nine times.
  1591.  
  1592.        2.  Edit TASKING.ADA to become your solution.  Make your changes in
  1593.            upper case.
  1594.  
  1595.        3.  Compile TASKING.ADA, link, and execute.
  1596.  
  1597.        4.  Compare your output with page 34 of these notes.  If there are any
  1598.            errors, go back to step 2.
  1599.  
  1600.        5.  When your output agrees with these notes, you've finished the
  1601.            assignment and will have a chance to compare your solution with
  1602.            ours.
  1603.  
  1604.                                                                       Page 34
  1605.  
  1606.              OUTPUT FROM TASKING.EXE AFTER MODIFICATION OF TASKING.ADA
  1607.  
  1608.        C>tasking
  1609.        Task number 1 is starting.
  1610.        Task number 2 is starting.
  1611.        Task number 3 is starting.
  1612.  
  1613.  
  1614.        Task number 1 is starting.
  1615.  
  1616.        Task number 2 is starting.
  1617.  
  1618.        Task number 1 is starting.
  1619.        Task number 3 is starting.
  1620.  
  1621.  
  1622.        Task number 1 is starting.
  1623.        Task number 2 is starting.
  1624.  
  1625.  
  1626.        Task number 1 is starting.
  1627.        Task number 3 is starting.
  1628.  
  1629.  
  1630.        C>
  1631.  
  1632.                                                                       Page 35
  1633.  
  1634.        TASKING.ANS
  1635.        -----------
  1636.  
  1637.        -- Our solution to Outside Assignment 6
  1638.        with text_io, calendar; use text_io, calendar;
  1639.        procedure tasking is
  1640.           interval        : constant duration := 5.0;
  1641.           total_intervals : constant positive := 9;
  1642.           start_time      : constant time := clock;
  1643.           quitting_time   : constant time := start_time +
  1644.                                                     total_intervals*interval;
  1645.           next_time       : time := start_time;
  1646.           task type tick is
  1647.              ENTRY IDENTIFY(TASK_NUMBER : IN NATURAL);
  1648.              entry shutdown;
  1649.           end tick;
  1650.           T               : ARRAY(1 .. 3) OF TICK;
  1651.           PERIOD          : CONSTANT ARRAY(T'RANGE) OF POSITIVE := (2, 3, 4);
  1652.           TIMER           : ARRAY(T'RANGE) OF NATURAL := (OTHERS => 0);
  1653.           task body tick is
  1654.              quit : boolean := false;
  1655.           begin
  1656.              while not quit loop
  1657.                 select
  1658.                    ACCEPT IDENTIFY(TASK_NUMBER : IN NATURAL) DO
  1659.                       PUT_LINE("Task number" & INTEGER'IMAGE(TASK_NUMBER) &
  1660.                            " is starting.");
  1661.                    END IDENTIFY;
  1662.                 or
  1663.                    accept shutdown;
  1664.                    quit := true;
  1665.                 end select;
  1666.              end loop;
  1667.           end tick;
  1668.        begin
  1669.           while next_time < quitting_time loop
  1670.              FOR I IN T'RANGE LOOP
  1671.                 IF TIMER(I) = 0 THEN
  1672.                    T(I).IDENTIFY(I);
  1673.                    TIMER(I) := PERIOD(I);
  1674.                 END IF;
  1675.                 TIMER(I) := TIMER(I) - 1;
  1676.              END LOOP;
  1677.              next_time := next_time + interval;
  1678.              delay next_time - clock; new_line;
  1679.           end loop;
  1680.           FOR I IN T'RANGE LOOP
  1681.              T(I).SHUTDOWN;
  1682.           END LOOP;
  1683.        end tasking;
  1684.  
  1685.                                                                       Page 36
  1686.  
  1687.                                DISCLAIMER OF WARRANTY
  1688.  
  1689.        We hate legal mumbo jumbo, but we have to say the following to protect
  1690.        ourselves:
  1691.  
  1692.        Software Innovations Technology makes no warranty of any kind,
  1693.        expressed or implied, including any warranties of merchantability or
  1694.        fitness for a particular purpose.  We shall not be liable for any
  1695.        damages, whether direct, indirect, incidental, special, or
  1696.        consequential, arising from a failure of this program to operate in a
  1697.        manner desired by the user.  We shall not be liable for any damage to
  1698.        data or property which may be caused directly or indirectly by use of
  1699.        this program.  IN NO EVENT SHALL SOFTWARE INNOVATIONS TECHNOLOGY BE
  1700.        LIABLE FOR ANY DAMAGES, INCLUDING ANY LOST PROFITS, LOST SAVINGS, OR
  1701.        OTHER INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR
  1702.        INABILITY TO USE THIS PROGRAM, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  1703.